import pygtk
import re
pygtk.require("2.0")
import gtk, gobject
from error import *

class WindowTemplate:
    COLOR_WRONG = "#ffecce"
    COLOR_EMPTY = "#ffffff"
    
    def __init__(self):
        self.validators = {}
        self.REGEX = {"REGEX_IP" : "^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$",
                      "REGEX_DOMAIN" : "^([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*)?$",
                      "REGEX_MOUNT_POINT" : "^\S+(/\S+)*",
                      "REGEX_PATH" : "^\S+(/\S+)*$",
                      "REGEX_LOGIN" : "^\S+$",
                      "REGEX_PASSWORD" : "^\S+$",
                      "REGEX_NAME" : "^.+$"}
            
            
    def add_validator(self,tb,regex,empty = False):
        valid = False
        self.validators[tb]=[regex,valid,empty]
        tb.connect("changed",self.is_valid_callback)
        self.is_valid_callback(tb)
        
    def is_valid_callback(self,tb):
        if self.validators[tb][0] in self.REGEX:
                REGEX = self.REGEX[self.validators[tb][0]]
        else:
            debug("Regex not found")
        
        if re.match(REGEX,tb.get_text()) or ( len(tb.get_text()) == 0 and self.validators[tb][2] == True):
            self.validators[tb][1] = True
            tb.modify_base(gtk.STATE_NORMAL,gtk.gdk.color_parse(self.COLOR_EMPTY))
        else:
            self.validators[tb][1] = False
            tb.modify_base(gtk.STATE_NORMAL,gtk.gdk.color_parse(self.COLOR_WRONG))

    def is_valid(self):
        for item in self.validators:
            if self.validators[item][1] == False:
                return False
        else:
            return True
        
    def build_hbox(self,widgets):
        """ 
        Input = tuple of widgets
        Outout = Horizontal Box with Widgets 
        """
        hbox = gtk.HBox(True, 5)
        for widget in widgets:
            hbox.pack_start(widget,False)
        return hbox
        
    def build_vbox(self,widgets):
        """ 
        Input = tuple of widgets
        Outout = Vertical Box with Widgets 
        """
        vbox = gtk.VBox(True, 5)
        for widget in widgets:
            vbox.pack_start(widget,False)
        return vbox
